package impl

import (
	"gitee.com/fzh-show/mcenter/apps/user"
	"gitee.com/fzh-show/mcenter/conf"
	"go.mongodb.org/mongo-driver/mongo"

	"gitee.com/fzh-show/mcenter/apps/token"
	"google.golang.org/grpc"

	"github.com/infraboard/mcube/ioc"
)

var _ token.Service = (*impl)(nil)

func init() {
	ioc.RegistryController(&impl{})
}

type impl struct {
	token.UnimplementedRpcServer

	ioc.IocObjectImpl
	// 依赖User业务实现
	user user.Service
	// 依赖mongo的collection
	col *mongo.Collection
}

func (i *impl) Init() error {
	i.user = ioc.GetController(user.AppName).(user.Service)

	// 补充mongo依赖
	db, err := conf.C().Mongo.GetDB()
	if err != nil {
		return err
	}
	i.col = db.Collection("tokens")
	return nil
}

func (i *impl) Name() string {
	return token.AppName
}

func (i *impl) Registry(server *grpc.Server) {
	token.RegisterRpcServer(server, i)
}
